package cwinsor.com.leaf_raking;


import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;


/**
 * @author cwinsor
 *
 */
public class RakerServices {

	// the list of rakers
	ArrayList<Raker> rakerList;

	// debug
	private int debugSection = 0;
	String dS = this.getClass().getSimpleName() + "----";


	/**
	 * constructor
	 */
	public RakerServices() {

		rakerList = new ArrayList<Raker>();

	}

	/* 
	 * add a raker to the list
	 * 
	 */
	public void addRaker(Raker raker) {

		rakerList.add(raker);
	}


	/*
	 * read in data from the csv file and internalize into a list
	 */
	public void readCsvFile() {

		System.out.println(dS + "reading csv file...");

		String csvInFile = "C:/Users/cwinsor/Documents/me/_BHRA_2013/leaf raking/schedule_files/rakers.csv";

		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";

		try {

			br = new BufferedReader(new FileReader(csvInFile));

			while ((line = br.readLine()) != null) {

				// read data into a string array, use comma as separator
				String[] theFields = line.split(cvsSplitBy);

				// convert to an ArrayList object
				ArrayList<String> theFieldsArrayObject = new ArrayList<String>();
				for (String s : theFields) {
					theFieldsArrayObject.add(s);
				}

				// debug print
				if (debugSection  == 1) {
					if (theFields[0].equals("skip")) {
						System.out.println(dS
								+ theFieldsArrayObject.get(0).toString());
					} else {
						System.out.println(dS
								+ theFieldsArrayObject.get(1).toString()
								+ " " + theFieldsArrayObject.get(2).toString());
					}
				}


				// add a raker
				// assume anything not marked "skip" "customer" or "supervisor"
				// is a raker
				if (    !theFields[0].equals("skip") &&
						!theFields[0].equals("customer") &&
						!theFields[0].equals("supervisor")) {

					// create a raker and put the data there
					Raker raker = new Raker();
					raker.addData(theFieldsArrayObject);
					rakerList.add(raker);
				}
			}
			System.out.println(dS + "there are " + rakerList.size() + " rakers");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		System.out.println(dS + "reading csv file DONE");

		// check
		this.check();
	}

	/*
	 * print the data to screen
	 */
	public Integer totalRakerPower() {
		Integer total = 0;
		for(Raker r: rakerList) {
			total += r.getPower();
		}
		return total;
	}	

	/*
	 * print the data to screen
	 */
	public void check() {
		System.out.println(dS + "checking... ");

		if (debugSection == 5) {
			for (Shift s: Shift.SHIFTS) {
				System.out.println(dS
						+ s.name());
			}
		}
		for (RakerColumns rakerColumns: RakerColumns.DAYS) {
			for (Team team: Team.TEAMS_NO_U) {
				for (Shift shift: Shift.SHIFTS) {

					// check that the number of rakers == 3
					RakerServices r = getRakersDayShiftTeam(rakerColumns,shift,team);
					if (r.count() != 3) {
						System.out.println("          NOTE --- "
								+ rakerColumns.name()
								+ " " + shift.name()
								+ " " + team.name()
								+ " has "
								+ r.count() + " rakers !!!");
					} else {
						System.out.println("          NOTE --- "
								+ rakerColumns.name()
								+ " " + shift.name()
								+ " " + team.name()
								+ " has "
								+ r.count() + " rakers");
					}

				}
			}
			System.out.println("");
		}
		System.out.println(dS + "checking done ");
	}


	/*
	 * print the data to screen
	 */
	public void print() {
		Iterator<Raker> iterator = rakerList.iterator();
		while (iterator.hasNext()) {
			Raker r = iterator.next();
			System.out.println(r.toStringCommaDelimitedAllFields());
		}
	}

	/*
	 * return a String of all contents in commma-separated-value format
	 */
	public String toCsvString(String in) {

		// debug print
		if (debugSection == 3) {
			System.out.println("---- toCsvString...  rakerList.size = " + rakerList.size());
		}
		Iterator<Raker> iterator = rakerList.iterator();
		while (iterator.hasNext()) {
			Raker r = iterator.next();

			in = in
					+ "," + "raker"
					+ "," + r.getFirst() 
					+ "," + r.getLast() 
					+ ","
					+ ",(c)" + r.getPhoneC()
					+ ",(h)" + r.getPhoneH();
			//+ "," +
			//		+ "," + r.getGender()
			//		+ "," + r.getVarsityNovCox()
			//		+ "," + r.getGrade();
			// availability by day/shift
			/*
			for (Day day : Day.DAYS) {
				for (Shift shift : Shift.SHIFTS) {
					in = in + "," + r.getAvailability(day, shift).toString();
				}
			}
			// assignment by day/shift
			for (Day day : Day.DAYS) {
				for (Shift shift : Shift.SHIFTS) {
					in = in + "," + r.getAssignment(day, shift).name();
				}
			}
			 */
			in = in + "\n";
		}
		// debug print
		if (debugSection == 3) {
			System.out.println("---- toCsvString...  done");
		}
		return in;
	}


	/*
	 * return a RakerList of rakers given day, team, shift
	 */
	public RakerServices getRakersDayShiftTeam(RakerColumns rakerColumns, Shift shift, Team team) {
		RakerServices theList = new RakerServices();

		// for each entry in the list
		for (Raker raker : rakerList) {
			if (raker.getAssignment(rakerColumns,shift).equals(team)) {
				theList.addRaker(raker); 
			}
		}
		return theList;	
	}


	/*
	 * count
	 * returns the number of rakers in the internal raker list
	 */
	public int count() {
		return rakerList.size();
	}

}